package org.langcom;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.prefs.Preferences;

import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;
import org.osgi.framework.BundleContext;

public class CommonsLangPlugin extends Plugin {

	private static CommonsLangPlugin plugin;
	private static String path;
	public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd.MM.yyyy HH.mm.ss"); //$NON-NLS-1$
	public static final String USER_DATA_PATH = "COMMON_USER_DATA"; //$NON-NLS-1$

	public CommonsLangPlugin() {
		super();
		plugin = this;
	}

	public void start(BundleContext context) throws Exception {
		super.start(context);
	}

	public void stop(BundleContext context) throws Exception {
		super.stop(context);
		plugin = null;
	}

	public static CommonsLangPlugin getDefault() {
		return plugin;
	}

	public static File getAppDataFolder() {
		return new File(getAppDataPath());
	}

	public static String getAppDataPath() {
		return getUserDataPath()
			+ File.separator
			+ Platform.getProduct().getName().replaceAll(" ", "_");
	}
	
	public static String getUserDataPath() {
		path = Preferences.userNodeForPackage(CommonsLangPlugin.class).get(
			CommonsLangPlugin.USER_DATA_PATH,
			path);
		if (path == null) {
			path = System.getProperty("user.home");
		}
		return path;
	}

	public static void setUserDataPath(String newPath) {
		path = newPath;
		Preferences.userNodeForPackage(CommonsLangPlugin.class).put(CommonsLangPlugin.USER_DATA_PATH, path);
		System.setProperty("user.home", path);
	}
}
